package com.zmykj.hik.hikService.Common;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


/**
 * 功能：XML解析工具类
 * @author LengChen
 * @version 1.0
 * @date 2021/5/26
 */
public class ParseXMLUtils {

    /**
     * 将Document对象转为Map（String→Document→Map）
     * @param document
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Map<String, Object> Dom2Map(Document document){
        Map<String, Object> map = new HashMap<String, Object>();
        if(document == null)
            return map;
        Element root = document.getRootElement();
        for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
            Element e = (Element) iterator.next();
            //System.out.println(e.getName());
            List list = e.elements();
            if(list.size() > 0){
                map.put(e.getName(), Dom2Map(e));
            }else
                map.put(e.getName(), e.getText());
        }
        return map;
    }

    /**
     * 将Element对象转为Map（String→Document→Element→Map）
     * @param element
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Map Dom2Map(Element element){
        Map map = new HashMap();
        List list = element.elements();
        if(list.size() > 0){
            for (int i = 0;i < list.size(); i++) {
                Element iter = (Element) list.get(i);
                List mapList = new ArrayList();
                if(iter.elements().size() > 0){
                    Map m = Dom2Map(iter);
                    if(map.get(iter.getName()) != null){
                        Object obj = map.get(iter.getName());
                        if(!obj.getClass().getName().equals("java.util.ArrayList")){
                            mapList = new ArrayList();
                            mapList.add(obj);
                            mapList.add(m);
                        }
                        if(obj.getClass().getName().equals("java.util.ArrayList")){
                            mapList = (List) obj;
                            mapList.add(m);
                        }
                        map.put(iter.getName(), mapList);
                    }else
                        map.put(iter.getName(), m);
                }
                else{
                    if(map.get(iter.getName()) != null){
                        Object obj = map.get(iter.getName());
                        if(!obj.getClass().getName().equals("java.util.ArrayList")){
                            mapList = new ArrayList();
                            mapList.add(obj);
                            mapList.add(iter.getText());
                        }
                        if(obj.getClass().getName().equals("java.util.ArrayList")){
                            mapList = (List) obj;
                            mapList.add(iter.getText());
                        }
                        map.put(iter.getName(), mapList);
                    }else
                        map.put(iter.getName(), iter.getText());//公共map resultCode=0
                }
            }
        }else
            map.put(element.getName(), element.getText());
        return map;
    }





    public static void main(String[] args) {
        String str1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<PPVSPMessage>\n" +
                "<Version>5.0</Version>\n" +
                "<Sequence>35</Sequence>\n" +
                "<CommandType>REQUEST</CommandType>\n" +
                "<Command>ACS</Command>\n" +
                "<Params>\n" +
                "<DeviceID>F80936533</DeviceID>\n" +
                "<Time>2021-05-26 09:26:37</Time>\n" +
                "<MajorType>0x5</MajorType>\n" +
                "<MinorType>0x26</MinorType>\n" +
                "<CardType>1</CardType>\n" +
                "<CardReaderNo>1</CardReaderNo>\n" +
                "<DoorNo>1</DoorNo>\n" +
                "<employeeNoString>1</employeeNoString>\n" +
                "<serialNo>439</serialNo>\n" +
                "<userType>normal</userType>\n" +
                "<currentVerifyMode>23</currentVerifyMode>\n" +
                "<currentEvent>ture</currentEvent>\n" +
                "<frontSerialNo>438</frontSerialNo>\n" +
                "<mask>unknown</mask>\n" +
                "<helmet>unknown</helmet>\n" +
                "<attendanceStatus>undefined</attendanceStatus>\n" +
                "<label></label>\n" +
                "<PurePwdVerifyEnable>true</PurePwdVerifyEnable>\n" +
                "</Params>\n" +
                "</PPVSPMessage>";

        SAXReader saxReader = new SAXReader();
        Document document;
        try {
            document = saxReader.read(new ByteArrayInputStream(str1.getBytes()));
            Map map = Dom2Map(document);
            System.out.println("map>>> " + map);
            System.out.println("map>>> " + ((Map)map.get("Params")).get("MinorType"));
            /*
            {DB_ID=EUR, CHANNEL_ID=11, USERNAME=tom, PASSWORD=sss, POOL_ID=2}
            {BODY={BUSLIST=[{TRACE_ID=97D2C7D26224A2DAE9A1CB501E60F395, PHONE_NO=7107300212, LANG=zh_CN, TENANT_ID=EUR}, {TRACE_ID=444424A2DAE9A1CB501E60F395, PHONE_NO=2222300212, LANG=zh_CN, TENANT_ID=USA}]}, HEADER={CHANNEL_ID=11, USERNAME=tom, PASSWORD=sss, POOL_ID=2}}
            */
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }


}
